package com.haizhi.crm.biz.service.impl;

import com.haizhi.crm.biz.model.UserModuleDetailVo;
import com.haizhi.crm.biz.query.QueryEngine;
import com.haizhi.crm.biz.service.UserModuleQueryService;
import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.common.util.CommonUtils;
import com.haizhi.crm.sys.configure.dao.UserModuleDao;
import com.haizhi.crm.sys.configure.model.UserModuleDo;
import com.haizhi.crm.sys.configure.model.UserModuleSettingVo;
import com.haizhi.crm.sys.configure.service.UserModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by caochao on 2018/06/03.
 */
@Service
public class UserModuleQueryServiceImpl implements UserModuleQueryService {

    @Resource
    private UserModuleDao userModuleDao;

    @Autowired
    private QueryEngine queryEngine;

    @Autowired
    private UserModuleService userModuleService;

    @Override
    public Response findUserModules() {
        List<UserModuleSettingVo> userModuleSettingVos = queryUserModule();
        List<UserModuleDetailVo> userModuleDetailVos = userModuleSettingVos.stream()
                .map(n -> queryEngine.query(n)).collect(Collectors.toList());
        return Response.success(userModuleDetailVos);
    }


    private List<UserModuleSettingVo> queryUserModule() {
        Long userId = CommonUtils.getUserID();
        List<UserModuleDo> userModuleDos = userModuleDao.findByUserId(userId);
        List<UserModuleSettingVo> userModuleSettingVos = userModuleDos.stream()
                .map(n -> userModuleService.findUserModuleSetting(n.getId()))
                .collect(Collectors.toList());
        return userModuleSettingVos;
    }
}
